<?php $this->load->view('header/home'); ?>
    <div id="main">
		<div class="container">
	        <div class="span-17 colborder" id="content">
	            <!-- charts placeholder -->
	            <div id="placeholder1" style="height:250px; display: none" class="span-16 last"></div> 
                <div id="placeholder2" style="height:250px; display: none" class="span-16 last"></div>
                <div id="placeholder3" style="height:250px; display: none" class="span-16 last"></div> 

	            <?php if (isset($messages)) echo $messages; ?>
	            
                <?php if (isset($flights)): ?>
                <fieldset id="latest-flights" class="span-16 last box prepend-top">
                    <legend class="caps">Latest Flights</legend>
                    <table border="0" cellpadding="2" cellspacing="1">
                    <thead>
                    <tr>
                        <td>#</td>
                        <td>Date</td>
                        <td>Takeoff</td>
                        <td>Total Hours</td>
                        <td>Distance</td>
                        <td>OLC</td>
                        <td>Valid</td>
                        <td>Year</td>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($flights as $f): ?>
                    <tr>
                        <td>&nbsp;</td>
                        <td><?php echo $f->date; ?></td>
                        <td><?php echo $f->Takeoff->name; ?></td>
                        <td><?php echo Converter::secs2Time($f->duration); ?></td>
                        <td><?php echo Converter::m2Km($f->flight_km); ?> Km</td>
                        <td><?php echo sprintf("%.2f", $f->flight_points); ?></td>
                        <td><?php echo $f->grecord? 'Yes':'No'; ?></td>
                        <td><?php echo $f->year; ?></td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                    </table>
                </fieldset>
                <?php endif; ?>

                <!-- div class="span-16 last box prepend-top">
                    <dl id="" class="span-8">
                        <dt class="caps"><?=lang('profile.Name')?>:</dt>
                        <dd><?php echo $person->name; ?></dd>
                        <dt class="caps prepend-top"><?=lang('profile.EMail')?>:</dt> 
                        <dd><?php echo $person->email_primary; ?></dd>
                        <dt class="caps prepend-top"><?=lang('profile.Gender')?>: </dt> 
                        <dd><?php switch ($person->gender) {
                                case 'M': echo lang('profile.Male'); break;
                                case 'F': echo lang('profile.Female'); break; 
                                case 'O': echo lang('profile.Other'); break;
                        };?></dd>
                        <dt class="caps prepend-top"><?=lang('profile.NAC_ID')?>: </dt>
                        <dd><?php echo $person->nac_type.' '.$person->nac; ?>
                        </dd>
                    </dl>
                    <div id="" class="span-7 last">
                        <dl>
                            <dt class="caps"><?=lang('profile.Alternative_Email')?>: </dt> 
                            <dd><?php echo $person->email_alternate; ?></dd>
                            <dt class="caps prepend-top"><?=lang('profile.Phone_Work')?>: </dt> 
                            <dd><?php echo $person->phone_work; ?></dd>
                            <dt class="caps prepend-top"><?=lang('profile.Phone_Home')?>: </dt> 
                            <dd><?php echo $person->phone_home; ?></dd>
                            <dt class="caps prepend-top"><?=lang('profile.Phone_Mobile')?>: </dt> 
                            <dd><?php echo $person->phone_mobile; ?></dd>
                        </dl>
                    </div>
                    <div id="" class="span-8">
                        <dl>
                            <dt class="caps prepend-top"><?=lang('profile.Address')?>: </dt> 
                            <dd><?php echo $person->address_line_1; ?></dd>
                            <dd><?php echo $person->address_line_2; ?></dd>
                            <dt class="caps prepend-top"><?=lang('profile.Location')?>: </dt> 
                            <dd><?php echo $person->location; ?></dd>
                        </dl>
                    </div>
                    <div id="" class="span-7 last">
                        <dl>
                            <dt class="caps prepend-top"><?=lang('profile.Postcode')?>: </dt> 
                            <dd><?php echo $person->postcode; ?></dd>
                            <dt class="caps prepend-top"><?=lang('profile.Country')?>: </dt> 
                            <dd><?php echo $person->country_code; ?></dd>
                        </dl>
                    </div>
                </div-->
	        </div> <!-- end content -->
          <div class="span-6 last" id="sidebar">
	        <?php $this->load->view('menu/sidebar', array('person' => $person, 'context' => 'home'))?>
            <div class="sidebar-box box" id="">
                <h3 class="section-title"><?php echo anchor('profile/club/index/'.$person->id, 'Hours Per Year') ?></h3>
                <div id="piecontainer1" style="height:200px" class="last"></div>
            </div>
          </div> <!-- end sidebar -->
		</div> <!-- end container -->
	    <script language="javascript" type="text/javascript"> 
	     $(document).ready(function() {
	    	 $('#latest-flights').hide();
	    	 $('#placeholder1').show();
	    	 $('#placeholder2').show();
	    	 $('#placeholder3').show();
		     
	    	 $.getJSON('<?php echo site_url(); ?>/profile/stats/ajax_stats/<?php echo $person->id;?>/<?php echo $pilot->leonardo_id?>', function(data) {
	    	        var data_count = [];
	    	        var data_duration = [];
	    	        var data_years = [];
	    	        var data_distance = [];
	    	        var series = [];
	    	        var years = [];
	    	        var series_years = [];

	    	        var colours = ['#1A1F26', '#D9C197', '#B2773F', '#BF4C1A', '#69787F', '#A8C0CC'];

	    	        $.each(data, function(i, value) {
	    	            var c = value["flights_count"];
	    	            var s = value["sum_duration"];
	    	            var d = value["flight_date"].replace('-', '/') + '/01'; 
	    	            var y = value["flight_year"];

	    	            years[y] = (years[y] == null? 0 : parseInt(years[y])) + parseInt(value["sum_duration"]);
	    	            series.push(value["flight_date"]);

	    	            len = series.length;

	    	            data_count.push(parseInt(value["flights_count"]));
	    	            data_duration.push((Math.round(value["sum_duration"]/3600)));
	    	            data_distance.push(parseInt(parseFloat(value["sum_distance"]/1000)));
	    	        });

	    	        var idx = 0;
	    	        for (i in years) {
		    	        data_years.push({
                            name: i,
                            y: Math.round(years[i] / 3600)//,
                            //color: colours[idx++]
                        });
                        series_years.push(i);
	    	        }

	    	        chart = new Highcharts.Chart({
	    	            chart: { renderTo: 'placeholder1', defaultSeriesType: 'column' },
	    	        title: { text: 'Hours / Flights' },
                    legend: { enabled: false },
	    	        xAxis: {
	    	            categories: series, 
	    	            labels: { rotation: -45, align: 'right', x: 10, y: 7, style: { font: 'normal 9px Verdana, sans-serif'} }
	    	        },
	    	        yAxis: [{
	    	            min: 0, title: { text: 'Flights' }
	    	        }, {
	    	            title: { text: 'Hours', margin: 40, style: { color: '#89A54E' } },
	    	            labels: { style: { color: '#4572A7' } }, 
	    	            opposite: true
	    	        }],
	    	        tooltip: {
	    	            formatter: function() {
	    	            	var s;
	                        if (this.point.name) { // the pie chart
	                            s = '<b>Hours In</b><br/>'+
	                                this.point.name +': ~'+ Highcharts.numberFormat(Math.ceil(this.y, 0));
	                        } else {
	                            s = '<b>'+ this.series.name +'</b><br/>'+
	                                this.x  +': '+ ((this.series.name == 'Hours')? Highcharts.numberFormat(this.y, 1) : this.y);
	                        }
	                        return s;
	    	            }
	    	        },
	    	        series: [{
	    	            name: 'Flights', data: data_count,
	    	            dataLabels: { enabled: true, rotation: -90, x: -3, y: 10,
	    	                formatter: function() { return Highcharts.numberFormat(this.y, 0); },
	    	                style: { font: 'normal 9px Verdana, sans-serif' } }
	    	        }, {
	    	            name: 'Hours', color: '#89A54E', type: 'spline', data: data_duration, yAxis: 1,
	    	            dataLabels: { enabled: true, 
	    	                formatter: function() { return Highcharts.numberFormat(this.y, 1); },
	    	        	    style: { font: 'normal 9px Verdana, sans-serif' } }
	    	        }]
	    	        });

	    	        chart = new Highcharts.Chart({
	    	            chart: {
	    	        	  renderTo: 'placeholder2', defaultSeriesType: 'spline'
	                    },
	                    title: { text: 'Distance / Flights' },
	                    legend: { enabled: false },
	                    series: [{
                            name: 'Flights', data: data_count, type: 'column',
                            dataLabels: { enabled: true, rotation: -90, x: -3, y: 10, style: { font: 'normal 9px Verdana, sans-serif' }}
                        },{
                            name: 'Distance', data: data_distance,
                            dataLabels: { enabled: true, style: { font: 'normal 9px Verdana, sans-serif' }},
                            yAxis: 1
                        }],
	                    xAxis: {
	                        categories: series,
	                        labels: { rotation: -45, align: 'right', x: 10, y: 7, style: { font: 'normal 9px Verdana, sans-serif' } }
	                    },
	                    yAxis: [{ 
                            title: { text: 'Flights' }
                        }, { 
		                    title: { text: 'Hours', margin: 35, style: { color: '#AA4643' } },
                            opposite: true
	                    }],
		                tooltip: {
	                        formatter: function() {
	                                s = '<b>'+ this.series.name +'</b><br/>'+
	                                    this.x  +': '+ ((this.series.name == 'Distance')? Highcharts.numberFormat(this.y, 1) : this.y);
	                            return s;
	                        }
	                    }
	    	        });

	    	        chart = new Highcharts.Chart({
                        chart: {
                          renderTo: 'placeholder3', defaultSeriesType: 'spline'
                        },
                        title: { text: 'Distance / Hours' },
                        legend: { enabled: false },
                        series: [{
                            name: 'Hours', data: data_duration, color: '#89A54E', type: 'column',
                            dataLabels: { enabled: true, style: { font: 'normal 9px Verdana, sans-serif' }, rotation: -90, x: -3, y: 10 }
                        },{
                            name: 'Distance', data: data_distance, color: '#AA4643',
                            dataLabels: { enabled: true, style: { font: 'normal 9px Verdana, sans-serif' }},
                            yAxis: 1
                        }],
                        xAxis: {
                            categories: series,
                            labels: { rotation: -45, align: 'right', x: 10, y: 7, style: { font: 'normal 9px Verdana, sans-serif' } }
                        },
                        yAxis: [{ 
                            title: { text: 'Hours' , style: { color: '#89A54E' } }
                        }, { 
                            title: { text: 'Distance', margin: 30, style: { color: '#AA4643' } },
                            opposite: true
                        }],
                        tooltip: {
                            formatter: function() {
                                    s = '<b>'+ this.series.name +'</b><br/>'+
                                        this.x  +': '+ ((this.series.name == 'Distance')? Highcharts.numberFormat(this.y, 1) : this.y);
                                return s;
                            }
                        }
                    });

	    	        var chart = new Highcharts.Chart({
	    	        	   chart: { renderTo: 'piecontainer1', defaultSeriesType: 'area', margin: [10, 50, 60, 30] },
	    	        	   title: { text: 'Hours per Year', style: { display: 'none' } },
	    	        	   legend: { enabled: false },
	    	        	   series: [{ 
		    	        	   data: data_years,
		    	        	   dataLabels: { enabled: true, style: { font: 'normal 9px Verdana, sans-serif' } } 
		    	           }],
	    	        	   xAxis: {
	                            categories: series_years,
	                            labels: { rotation: -45, align: 'right', x: 10, y: 7, style: { font: 'normal 9px Verdana, sans-serif' } }
	                       },
	                       yAxis: {
	                           min: 0, title: null
	                       }
	    	        	});
			 });
		});
	    </script> 
    </div> <!-- end main -->
<?php $this->load->view('footer/standard');?>
